<template>
  <div @keyup.enter="getData()">
    <el-form class="query-table-form" :inline="true">
      <el-form-item>
        <el-input v-model="params.customer_name" placeholder="主体名称查询" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="params.corp_name" placeholder="负责人查询" :clearable="true"></el-input>
      </el-form-item>
      <el-form-item>
        <select-address
          v-model="params.address_code"
          placeholder="地址查询"
          auto-top
          clearable
          @change="getData()"
        ></select-address>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="getData()">查询</el-button>
      </el-form-item>
    </el-form>
    <c-table border v-loading="loading" :columns="tableColumns" :data="tableData">
      <template v-slot:address="scope">
        <div class="text-truncate" :title="scope.row.address">{{ scope.row.address }}</div>
      </template>
      <template v-slot:itemStatus="scope">
        <span class="badge" :class="`badge-${scope.row.itemStatusStyle}`">
          {{ scope.row.itemStatusLabel }}
        </span>
      </template>
      <template v-slot:name="scope">
        <el-popover placement="top-start" title="项目经理" trigger="hover">
          <div>
            <div>{{ `A角： ${scope.row.res_a_name || '-'}；` }}</div>
            <div class="mb-2">{{ `电话： ${scope.row.res_a_phone || '-'}` }}</div>
            <div>{{ `B角： ${scope.row.res_b_name || '-'}；` }}</div>
            <div>{{ `电话： ${scope.row.res_b_phone || '-'}` }}</div>
          </div>
          <div slot="reference">
            <div class="status-label badge-info text-left w-100">
              <div>A角:{{ scope.row.res_a_name }}</div>
              <div>B角:{{ scope.row.res_b_name }}</div>
              <div>风控员:{{ scope.row.res_person_risk_point }}</div>
            </div>
          </div>
        </el-popover>
      </template>
    </c-table>
    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="totalCount"
        @current-change="getData"
      ></el-pagination>
    </div>

    <el-dialog
      :visible.sync="reportVisible"
      :close-on-click-modal="false"
      append-to-body
      top="30px"
      width="80%"
    >
      <el-tabs v-model="active">
        <el-tab-pane label="项目信息" name="first">
          <info-view :related-data="relatedData"></info-view>
        </el-tab-pane>
        <el-tab-pane label="签约登记表">
          <contract-registr :query-id="queryId"></contract-registr>
        </el-tab-pane>
        <el-tab-pane label="合同审批表">
          <contract-approve
            :query-id="queryId"
            :process-status="relatedData.process_status"
            view-mode
          ></contract-approve>
        </el-tab-pane>
        <!-- <el-tab-pane label="项目审批表">
          <investigate-approve-view :query-id="queryId"
                                    :process-status="relatedData.process_status"></investigate-approve-view>
        </el-tab-pane> -->
        <el-tab-pane label="合同信息" name="contract">
          <contract-guard-view :query-id="queryId"></contract-guard-view>
        </el-tab-pane>
        <el-tab-pane label="担保意向函" name="letter" v-if="+relatedData.ele_letter_status > 0">
          <letter-view :query-id="queryId"></letter-view>
        </el-tab-pane>
        <el-tab-pane label="附件">
          <attach-view :queryId="queryId" :visible="reportVisible"></attach-view>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>
    <attach-cmpt :visible.sync="attachVisible" :customer-related-id="attachRelatedId"></attach-cmpt>

    <handle-contract :visible.sync="handleVisible" :query-id="handleId"></handle-contract>
  </div>
</template>

<script>
import { enumGuatype, enumProjectResStatus, enumProcessStatus } from '@/credit/plugins/enumTown'
import { baseTableColumns } from '@/credit/views/province/mission/UnionNo'
import baseMixins from '@/bigcredit/views/self/fast/mixins'

import SelectAddress from '@/components/select/SelectAddress'
import AttachView from '@/credit/components/AttachView'
import AttachCmpt from '@/credit/views/city/program/Attach'

// import InvestigateApproveView from '@/credit/views/county/unionProvince/components/InvestigateApproveView'
import ContractApprove from '@/bigcredit/views/self/fast/contract/components/Main'
import LetterView from '@/credit/views/county/unionProvince/components/LetterView'
import ContractGuardView from '@/credit/views/county/unionProvince/components/ContractGuardView'
import ContractRegistr from '@/credit/views/city/self/components/contractRegistr/ContractRegistr'
import InfoView from '@/credit/views/city/bus/normal/Info'
import HandleContract from '@/credit/views/union/contract/component/HandleContract'

export default {
  components: {
    SelectAddress,
    AttachView,
    AttachCmpt,
    // InvestigateApproveView,
    ContractApprove,
    LetterView,
    ContractGuardView,
    ContractRegistr,
    InfoView,
    HandleContract,
  },
  mixins: [baseMixins],
  data() {
    return {
      enumProjectResStatus,
      order: {
        approved_date: 'desc',
      },
      params: {
        index: 1,
        size: 15,
        address_code: '',
        customer_name: '',
        corp_name: '',
        'project_res_status.eq': 2,
        'contract_approval_status.gte': 2,
        'confuse_status.neq': 3,
        subKey: 'entrust_contract.customer_related_id',
      },
      tableData: [],
      tableColumns: [
        ...baseTableColumns,
        { attrs: { label: '审批金额(万元)', prop: 'replyFunds', align: 'center' } },
        { attrs: { label: '签约时间', prop: 'signRegisterTime', width: 140 } },
        {
          attrs: { label: '放款银行', prop: 'replyBank', align: 'center' },
          process: v => v || '-',
        },
        // { attrs: { label: '审批模式', prop: 'processLabel', width: 80, align: 'center' } },
        { attrs: { label: '项目经理' }, slot: 'name' },
        {
          attrs: { label: '待处理人员', prop: 'itemStatusLabel', width: 100, align: 'center' },
        },
        {
          attrs: { label: '操作', align: 'center', width: 140 },
          operations: [
            // { content: '上传附件', icon: 'fa fa-clipboard', event: this.hanleAttach },
            {
              content: '合同处理',
              icon: 'fa fa-file-text',
              event: this.handleContract,
              ifRender: row =>
                +row.contract_approval_status > 1 &&
                [row.res_a, row.res_b, row.res_user_id_risk_point].includes(this.userId) &&
                +row.loan_approval_status < 3,
            },
            { content: '查看', icon: 'fa fa-search', event: this.handleView },
          ],
        },
      ],

      editRow: {},
      editVisible: false,
      reportVisible: false,
      relatedData: {},
      attachVisible: false,
      attachRelatedId: '',
      queryId: '',
      active: 'first',
      editData: {},
      handleVisible: false,
      handleId: '',
    }
  },
  computed: {},
  watch: {},
  mounted() {},
  created() {
    this.getData()
  },
  methods: {
    handleContract(row = {}) {
      this.handleId = row.customer_related_id
      this.handleVisible = true
    },
    handleEdit(row = {}) {
      this.editData = { ...row }
      this.editVisible = true
    },
    dataAdapter(item) {
      item.guaType = enumGuatype[item.gua_type] || '-'
      item.signRegisterTime = this.$utils.dateFormat(item.sign_register_time, this.dateFmt)
      const approval = item.entrust_contract?.[0] || {}
      item.replyFunds = approval.reply_funds || ''
      item.replyBank = approval.bank_name || ''
      item.processLabel = enumProcessStatus[+item.process_status] || ''

      item.itemStatusLabel =
        +item.contract_approval_status > 2
          ? '已完成'
          : item.sign_register_time
          ? '待客户签约'
          : `选择签约方式`
      return item
    },
    hanleAttach(row) {
      this.attachVisible = true
      this.attachRelatedId = row.customer_related_id
    },
    handleView(row) {
      this.active = 'first'
      this.queryId = row.customer_related_id
      this.relatedData = row
      this.reportVisible = true
    },
  },
}
</script>
<style lang="scss" scoped>
.status-label {
  display: inline-block;
  padding: 0.25em 0.4em;
  font-size: 75%;
  font-weight: 700;
  line-height: 1;
  text-align: center;
  vertical-align: baseline;
  border-radius: 0.25rem;
  transition: color 0.15s;
}
</style>
